package cn.ekuma.epos.android.finder;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.openbravo.bean.Product;
import com.openbravo.bean.ProductCategory;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFCompareEnum;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import cn.ekuma.android.ui.filter.RefSelectFilter;
import cn.ekuma.android.ui.filter.RefSelectFilter.RefSelectedListener;
import cn.ekuma.data.ui.android.AbstractViewBeanListAdapter;
import  cn.ekuma.data.ui.android.AbstractFinderActivate;
import cn.ekuma.data.ui.android.AbstractTimeCycleListenerImplListActivate;
import cn.ekuma.epos.android.adapter.ProductListAdapter;
import cn.ekuma.epos.datalogic.DataLogicPDA;
import cn.ekuma.epos.datalogic.I_DataLogicERP;

public class ProductFinderActivate extends AbstractTimeCycleListenerImplListActivate<Product> {

    public static final int SELECT_PRODUCTCATEGORY=3;
	
	public static final int MSG_SUCCESS = 0;
    public static final int MSG_FAILURE = 1;

	private static final int SHARDPRODUCT_ID = Menu.FIRST;
	private static final int PREFPRODUCT_ID = Menu.FIRST + 1;
	private RefSelectFilter advFilter;
	
	private ProductCategory productCategory;

	public static final String LIST_PRODUCTS="list_products";
	List<Product> listObjs;

    protected ProductFinderActivate() {
       super(DataLogicPDA.getDataLogic());
    }

    @Override
	protected void onCreate(Bundle savedInstanceState) {
		Bundle mBundle=getIntent().getExtras();
		if(mBundle!=null){
		   listObjs=(List<Product>) mBundle.getSerializable(LIST_PRODUCTS);
		}
		 if(listObjs==null)
			   listObjs=new ArrayList();
		super.onCreate(savedInstanceState);
	}
	
	public AbstractViewBeanListAdapter getListAdapter() {
		return new ProductListAdapter(this,listObjs);
	}

	
	public void baseBeanSelected(Product obj, int position) {
		Intent result = new Intent();
		result.putExtra(SELECTED_BEAN, obj);
		setResult(RESULT_OK, result);
		finish();
	}
	
	public View createAdvFilterPanel() {
		 advFilter=new RefSelectFilter(this,SELECT_PRODUCTCATEGORY,"分类");
		 advFilter.setRefSelectedListener(new RefSelectedListener(){
			
			public void refSelected(RefSelectFilter filter, int requestCode) {
				Intent intent = new Intent();
				intent.setClass(ProductFinderActivate.this,ProductCategoryFinderActivate.class);
				startActivityForResult(intent, requestCode);
			} 
		 });
		return advFilter;
	}
	
	 public boolean onCreateOptionsMenu(Menu menu) {
		   super.onCreateOptionsMenu(menu);
		   menu.add(0, SHARDPRODUCT_ID, 0, "共享商品").setIcon(android.R.drawable.ic_menu_share);
		   menu.add(0, PREFPRODUCT_ID, 0, "特价商品").setIcon(android.R.drawable.ic_menu_delete);
		  return true;
	 }

	 public boolean onOptionsItemSelected(MenuItem item) {
		    switch (item.getItemId()) {
		      case SHARDPRODUCT_ID:
		    	  showShardProduct();
		    	  break;
		      case PREFPRODUCT_ID:
		    	  showPrefProduct();
		    	  break;
		    }
		    return true;
		  }
	 
	 private void showPrefProduct(){
		 
	 }
	 
	 private void showShardProduct(){
		 final ProgressDialog dialog = ProgressDialog.show(this, "", 
			        "查旬中，请稍等 …", true, true);
			new Thread(){
				
				public void run(){
					I_DataLogicERP dlERP=DataLogicPDA.getDLERP();
					try {
						List<Product> lists=dlERP.listProductByShared();
						if(lists==null)
							lists=new ArrayList();
						mHandler.obtainMessage(MSG_SUCCESS, lists).sendToTarget();			
					} catch (BasicException e) {
						e.printStackTrace();
						mHandler.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败   
					}
					dialog.dismiss(); 
				}
				}.start();		
	 }
	 
	 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		
		if(resultCode==RESULT_OK){
			switch(requestCode){
	    	   case SELECT_PRODUCTCATEGORY:
	    		   productCategory=(ProductCategory) data.getSerializableExtra(AbstractFinderActivate.SELECTED_BEAN);
	    		   advFilter.setSelectedBeanText(productCategory.getName());
	    		   break;
	    	}
		}else{
			switch(requestCode){
			case SELECT_PRODUCTCATEGORY:
	    		   productCategory=null;
	    		   advFilter.setSelectedBeanText(null);
	    		   break;
	    	}
		}
	}

	public void timeCycleChanged(Date startdate, Date enddate) {
		Object[] afilter=(Object[])createValue();
		afilter[10] = (startdate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_GREATEROREQUALS;
        afilter[11] = startdate;
        afilter[12] = (enddate == null) ? QBFCompareEnum.COMP_NONE : QBFCompareEnum.COMP_LESS;
        afilter[13] = enddate;
        query(afilter);
	}
	 
	
	public void filterParaReset() {
		productCategory=null;
		advFilter.setSelectedBeanText(null);
		quickTimeFilter.resertValues();
	}

	
	public void initAdvSeachView() {
		// TODO Auto-generated method stub
	}

	
	public boolean isQuickSeachAble() {	
		return true;
	}
	
	private void query(final Object afilter){
		 final ProgressDialog dialog = ProgressDialog.show(this, "", 
			        "查旬中，请稍等 …", true, true);
			new Thread(){
				
				public void run(){
					I_DataLogicERP dlERP=DataLogicPDA.getDLERP();
					try {
						List<Product> lists=dlERP.getProductList(afilter);
						if(lists==null)
							lists=new ArrayList();
						mHandler.obtainMessage(MSG_SUCCESS, lists).sendToTarget();			
					} catch (BasicException e) {
						e.printStackTrace();
						mHandler.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败   
					}catch (Exception e) {
                        e.printStackTrace();
                        mHandler.obtainMessage(MSG_FAILURE).sendToTarget();//获取图片失败
                    }
					dialog.dismiss(); 
				}
				}.start();		
	}
	
	
	public void onQuickSeach(String inputText) {
		Object[] afilter=(Object[])createValue();
		afilter[0] = QBFCompareEnum.COMP_RE;
        afilter[1] = inputText;
       //afilter[8] = QBFCompareEnum.COMP_RE;
       //afilter[9] = "%" + m_jtxtBarCode.getText() + "%";
        
        query(afilter);
	}
	
	 private Object createValue(){

	        Object[] afilter = new Object[14];

	        // name
	           afilter[0] = QBFCompareEnum.COMP_NONE;
	           afilter[1] = null;

	        // Precio de compra
	        afilter[2]  = QBFCompareEnum.COMP_NONE;
	        afilter[3] = null;

	        // Precio de venta
	        afilter[5] =null;
	        afilter[4] = QBFCompareEnum.COMP_NONE ;

	        // Categoria
	        if (productCategory==null) {
	            afilter[6] = QBFCompareEnum.COMP_NONE;
	            afilter[7] = null;
	        } else {
	            afilter[6] = QBFCompareEnum.COMP_EQUALS;
	            afilter[7] = productCategory.getID();
	        }

	        //BarCode
	         afilter[8] = QBFCompareEnum.COMP_NONE;
	         afilter[9] = null;
	        

	        // Date range

	        afilter[10] = QBFCompareEnum.COMP_NONE ;
	        afilter[11] = null;
	        afilter[12] =  QBFCompareEnum.COMP_NONE ;
	        afilter[13] = null;
	        
	        return afilter;
	    }
}
